

//action:d  img_x:0  img_y:0 imgw:100 imgh:208
//action:u  img_x:957  img_y:0 imgw:100 imgh:208

import g1 from "../sound/g1.wav";
import {getrandomaction} from "../comm";
import {Fighter} from "./fighter";
import {Actionpic} from "./actionpic";
import {Action} from "./action";

export class Soldier {
    team:number=0;
    fun:string="l" //上 下 左 右
    live:number=50;
    livelimit:number=200;
    demage:number=5;
    demagemin:number=5;
    demagemax:number=20;
    isfire:boolean=false;
    x:number=0;
    y:number=0;
    ratewidth:number=30;
    rateheight:number=40;
    speed:number=5;
    simagestr:any=null;
    sdoactions:Array<Action>=new Array<Action>();
    sdadio=new Audio();

    setactionindex=(atype:string)=>{
        for(let i=0;i<this.sdoactions.length;i++)
        {
            if(this.sdoactions[i].type==atype)
            {
                this.sdoactions[i].actionindex=0;
                break;
            }
        }
    }

    conmdraw=(atype:string,ptctx:any,imagesod:any)=>{
        for(let i=0;i<this.sdoactions.length;i++) {
            if(this.sdoactions[i].type==atype) {
                let actiond:Action=this.sdoactions[i];
                ptctx?.drawImage(imagesod, actiond.actionpics[actiond.actionindex].x,
                    actiond.actionpics[actiond.actionindex].y,
                    actiond.actionpics[actiond.actionindex].awidth,
                    actiond.actionpics[actiond.actionindex].aheight,
                    this.x, this.y, this.ratewidth, this.rateheight
                );
                break;
            }
        }
        for(let i=0;i<this.sdoactions.length;i++) {
            if (this.sdoactions[i].type == atype) {
                if (this.sdoactions[i].actionindex < this.sdoactions[i].actionpics.length-1) {
                    this.sdoactions[i].actionindex = this.sdoactions[i].actionindex + 1;
                } else {
                    this.sdoactions[i].actionindex = 0;
                }
                break;
            }
        }
    }

    imagesod = new Image();
    ini=()=>{
        this.fun="d";
        this.simagestr=require("../images/enemy1.png");
        this.imagesod = new Image();
        this.imagesod.src = this.simagestr;
        this.demage=Math.floor(Math.random() * (this.demagemax - this.demagemin + 1)) + this.demagemin;
        this.x=Math.floor(Math.random() * (600 - 0 + 1)) + 0;
        this.y=Math.floor(Math.random() * (280 - 100 + 1)) + 100;
        this.speed = Math.floor(Math.random() * (10 - 2 + 1)) + 2;
        this.rateheight=40;
        this.ratewidth=30;
        this.sdadio.src=g1;
        //通用动作 d
        let d_action:Action=new Action();
        d_action.actionindex=0;
        d_action.type="d";
        d_action.name="下";
        d_action.imgsrc=this.simagestr;
        for(let i=0;i<4;i++)
        {
            let tempaction:Actionpic=new Actionpic(0,i*208,100,208);
            d_action.actionpics.push(tempaction);
        }
        this.sdoactions.push(d_action);

        //通用动作 u
        let u_action:Action=new Action();
        u_action.actionindex=0;
        u_action.type="u";
        u_action.name="上";
        u_action.imgsrc=this.simagestr;
        for(let i=0;i<4;i++)
        {
            let tempaction:Actionpic=new Actionpic(957,i*208,100,208);
            u_action.actionpics.push(tempaction);
        }
        this.sdoactions.push(u_action);

        //动作 l
        //动作 f
        //射击
        let fire_action:Action=new Action();
        fire_action.actionindex=0;
        fire_action.type="fire";
        fire_action.name="射击";
        fire_action.imgsrc=this.simagestr;
        for(let i=4;i<6;i++)
        {
            let tempaction:Actionpic=new Actionpic(0,i*208,100,208);
            fire_action.actionpics.push(tempaction);
        }
        this.sdoactions.push(fire_action);

        //死亡动作
        let cy=1434;
        let dead_action:Action=new Action();
        dead_action.actionindex=0;
        dead_action.type="dead";
        dead_action.name="死亡";
        dead_action.imgsrc=this.simagestr;
        dead_action.actionpics.push(new Actionpic(0,cy,100,208));
        dead_action.actionpics.push(new Actionpic(192,cy,100,208));
        dead_action.actionpics.push(new Actionpic(384,cy,100,208));
        dead_action.actionpics.push(new Actionpic(576,cy,100,208));
        dead_action.actionpics.push(new Actionpic(768,cy,100,208));
        dead_action.actionpics.push(new Actionpic(0,cy+208,100,104));
        this.sdoactions.push(dead_action);
    }


    setaction=(fighter:Fighter)=> {
        if (this.live > 0) {
            let flag: string = getrandomaction();
            if (flag != "fire") {
                if(flag!=this.fun) {
                    this.fun = flag;
                    this.isfire=false;
                    if (this.fun == "u") {
                        this.setactionindex("u");
                    } else  {
                        //d
                        this.setactionindex("d");
                    }
                }

            } else {
                this.sdadio.play();
                this.isfire = true;
                this.setactionindex("fire");
                //射中机率
                let tmpflag = Math.floor(Math.random() * (70 - 0 + 1)) + 0;
                if (tmpflag > 50) {
                    fighter.live = fighter.live - this.demage;
                }

            }
        }
    }

    drawaction=(ptctx:any)=> {

        if (this.live > 0) {
            if (!this.isfire) {
                this.isfire = false;
                if (this.fun == "d") {
                    if (this.y < 300 - this.rateheight)
                        this.y = this.y + this.speed;
                    else {
                        this.fun = "u";
                        this.setactionindex("u");
                    }
                    this.conmdraw("d",ptctx,this.imagesod);
                }// else if (this.fun == "u") {
                else {
                    if (this.y > this.rateheight)
                        this.y = this.y - this.speed;
                    else {
                        this.fun = "d";
                        this.setactionindex("d");
                    }
                    this.conmdraw("u",ptctx,this.imagesod);
                }
            }else
            {
                this.conmdraw("fire",ptctx,this.imagesod);
            }
        }
    }

    drawdead=(ptctx:any)=>{
        let imagesod = new Image();
        imagesod.src = this.simagestr;
        this.conmdraw("dead",ptctx,imagesod);
    }

    bittarget=(target_x:number,target_y:number,target_width:number,target_height:number)=>{

    }
}

